using System.Collections.Generic;
using UnityEngine;
using Utils;

public class MapController : MonoBehaviour
{
    public List<GameObject> mapList;
    public int mapIndex;

    public void CreateChildMaps()
    {
        if (GameController.Instance().archiveCtr.levelData.gameType == GameType.Story)
        {
            PrefabEntity map = PrefabRoot.StoryMap;
            GameController.Instance().archiveCtr.levelData.mapList.Add(map);
        }

        int[] mapNum = {1, 2,},
            typeBase = {1, 2, 3,};
        int[] typeOrder = new RandomArray<int>(typeBase).GetDisruptedItems();

        mapList = new List<GameObject>();
        foreach (var i in typeOrder)
        {
            foreach (var j in new RandomArray<int>(mapNum).GetDisruptedItems())
            {
                PrefabEntity mapPrefab = PrefabRoot.GenerateMap(i, j);
                GameController.Instance().archiveCtr.levelData.mapList.Add(mapPrefab);
            }
            PrefabEntity mapBossPrefab = PrefabRoot.GenerateMap(i, 3);
            GameController.Instance().archiveCtr.levelData.mapList.Add(mapBossPrefab);
        }
        PrefabEntity mapEndPrefab = PrefabRoot.GenerateMapEnd(1);
        GameController.Instance().archiveCtr.levelData.mapList.Add(mapEndPrefab);
    } 

    public void TurnToNextMap()
    {
        mapList[mapIndex].SetActive(false);
        Destroy(mapList[mapIndex]);
        mapList[++mapIndex].SetActive(true);
        Vector3 pos = UITool.GetChildGameObject("StartPoint", mapList[mapIndex]).transform.position;
        GameController.Instance().playerCtr.SetPosition(pos);
        GameController.Instance().smallMap.ResetSmallMap();
        GameController.Instance().archiveCtr.TempLevelData.Level = mapIndex;
    }

    public Transform CurMap()
    {
        return mapList[mapIndex].transform;
    }
    
    public void CreateStoryMap()
    {
        PrefabEntity map = PrefabRoot.StoryMap;
        GameController.Instance().archiveCtr.levelData.mapList.Add(map);
    }
}